package com.kcsj.service;


import com.kcsj.dao.InventoryDao;
import com.kcsj.dao.ProductModelDao;
import com.kcsj.dao.WarehouseOutRecordDao;
import com.kcsj.domain.Inventory;
import com.kcsj.domain.WarehouseOutRecord;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class WarehouseOutRecordService {
    private static WarehouseOutRecordService warehouseOutRecordService = new WarehouseOutRecordService();
    public static WarehouseOutRecordService getInstance(){return warehouseOutRecordService;}
    private WarehouseOutRecordDao warehouseOutRecordDao = WarehouseOutRecordDao.getInstance();
    private ProductModelDao productModelDao = ProductModelDao.getInstance();
    private InventoryDao inventoryDao = InventoryDao.getInstance();

    public WarehouseOutRecord find(int id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        try{
            WarehouseOutRecord warehouseOutRecord;
            warehouseOutRecord = warehouseOutRecordDao.find(id,conn);
            return warehouseOutRecord;
        }catch (SQLException e){
            conn.rollback();
            throw e;
        }finally {
            conn.setAutoCommit(true);
        }
    }

    public Collection<WarehouseOutRecord> findAll(List<Condition> conditionList) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        try {
            Collection<WarehouseOutRecord> warehouseOutRecords;
            warehouseOutRecords = warehouseOutRecordDao.findAll(conditionList, conn);
            return warehouseOutRecords;
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.setAutoCommit(true);
        }
    }


    public int add(WarehouseOutRecord warehouseOutRecord,Integer amount) throws SQLException {
        //获取连接
        Connection conn = JdbcHelper.getConn();
        try {
            //取消自动提交，开始事务
            conn.setAutoCommit(false);
            Inventory inventory = new Inventory();
            inventory.setProductModel(warehouseOutRecord.getProductModel());
            inventory.setWarehouse(warehouseOutRecord.getProductModel().getWarehouse());
            inventory.setAmount(-amount);
            //调用dao的增加方法
            int affectedRows = warehouseOutRecordDao.add(warehouseOutRecord,conn);
            inventoryDao.update(inventory, conn);
            return affectedRows;
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.setAutoCommit(true);
        }
    }

    public boolean addAll(List<WarehouseOutRecord> warehouseOutRecords) throws SQLException {
        //获取连接
        Connection conn = JdbcHelper.getConn();
        try {
            //取消自动提交，开始事务
            conn.setAutoCommit(false);
            //调用dao的增加方法
            return warehouseOutRecordDao.addAll(warehouseOutRecords,conn);
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.setAutoCommit(true);
        }
    }

}
